package com.yumingjiang.weichatgtp.advice;

import com.alibaba.fastjson.JSONObject;
import com.yumingjiang.commontool.util.HttpClientResult;
import com.yumingjiang.commontool.util.LoggerUtil;
import com.yumingjiang.weichatgtp.common.eunms.SystemResultStutasEnum;
import com.yumingjiang.weichatgtp.common.exception.AuthException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Administrator]
 * @version : [v1.0]
 * @createTime : [2023/4/5 23:30]
 */
@ControllerAdvice
public class WebAdvice {
    private static final Logger LOGGER = LoggerFactory.getLogger(WebAdvice.class);

    @ResponseBody
    @ExceptionHandler(AuthException.class)
    public HttpClientResult globalAuthExceptionHandler(HttpServletResponse response, AuthException ex) {
        LoggerUtil.error(LOGGER, ex, ex.getMsg());
        String code = ex.getCode();
        String msg = ex.getMsg();
        HttpClientResult httpClientResult = new HttpClientResult();
        if ("403".equals(code)){
            httpClientResult.setSuccess(false);
            httpClientResult.setCode(SystemResultStutasEnum.AUTH_FAIL.getCode());
            httpClientResult.setMessage(SystemResultStutasEnum.AUTH_FAIL.getMessage()+"["+msg+"]");

        }
        return httpClientResult;
    }
}
